
global g_hDllModule,_SetGlobalHook,_UnsetGlobalHook

section .data

LC0:
	db "check stack fail!",0ah,0
	
g_hHook:
	db 0
	
g_hDllModule:
	db 0

extern _CallNextHookEx@16,_printf,_exit,_SetWindowsHookExA@16,_UnhookWindowsHookEx@4,\
_GetLastError@0

section .text 
_GetMsgProc:
	push ebp
	mov ebp,esp
	sub esp,0x1000
	
	push ebx
	push esi
	push edi 
	
	; [ebp+8] code
	; [ebp+12] wParam
	; [ebp+16] lParam 
	
	mov esi,esp 
	mov eax,[ebp+16] ; lParam
	push eax
	mov eax,[ebp+12] ; wParam
	push eax
	mov eax,[ebp+8] ; code
	push eax
	push g_hHook
	call _CallNextHookEx@16 
	cmp esi,esp
	call _cs 	
		
	pop edi
	pop esi
	pop ebx
	add esp,0x1000
	leave
	ret 
	
_SetGlobalHook:
	push ebp
	mov ebp,esp 
	sub esp,0x1000
	
	push ebx
	push esi 
	push edi 
	
	mov esi,esp
	push 0
	mov edi,g_hDllModule
	mov eax,[edi]
	push eax 
	push _GetMsgProc
	push 3
	call _SetWindowsHookExA@16
	cmp esi,esp
	call _cs 
	
	call _GetLastError@0
	
	mov edi,g_hHook
	mov [edi],eax 
	
	cmp dword [edi], 0 
	jne .LSetGlobalHook_400
	
	mov eax,0
	jmp .LSetGlobalHook_1
	
.LSetGlobalHook_400:
	mov eax,1
		
.LSetGlobalHook_1:	
	pop edi 
	pop esi 
	pop ebx 	
	add esp,0x1000 
	leave
	ret 
	
_UnsetGlobalHook:
	push ebp
	mov ebp,esp 
	sub esp,0x1000
	
	push ebx
	push esi
	push edi
	
	mov edi,g_hHook
	mov eax,[edi]
	test eax,eax
	je .LUnsetGlobalHook_10
	
	mov esi,esp 
	push g_hHook
	call _UnhookWindowsHookEx@4
	cmp esi,esp 
	call _cs 
	
.LUnsetGlobalHook_10:	
	mov eax,1
	pop edi
	pop esi
	pop ebx 
	add esp,0x1000
	leave
	ret 
	
_cs:
	je .Lcs_0
	
	push LC0 
	mov eax,0 
	call _printf 
	
	push 0
	call _exit 
	
.Lcs_0:
	ret 
	
	
_DllMain@12:
	push ebp
	mov ebp,esp 
	sub esp,0x1000 
	
	push ebx 
	push esi
	push edi 
	
	; [ebp+8] hModule 
	; [ebp+12] ul_reason_for_call
	; [ebp+16] lpReserved
	
	cmp dword [ebp+12], 1 ; ul_reason_for_call
	jne .LDllMain_10
	
	mov eax,[ebp+8] ; hModule
	mov edi,g_hDllModule
	mov [edi],eax 
	
.LDllMain_10:	
	mov eax,1
	pop edi 
	pop esi
	pop ebx 
	add esp,0x1000
	leave
	ret 